<template>

        <el-container class="home-container">
            <!-- 头部区域 -->
           <el-header>
               <div>
                   <img src="../assets/img/MiuKiss.png" alt="">
                   <span>电商后台管理系统</span>
               </div>
               <el-button type="info" @click="logout">退出</el-button>
           </el-header>
           
           <!-- 页面主体 -->
               <el-container>
                   <!-- 侧边栏 -->
                  <el-aside :width="sideWidth">
                      <div class="toggle-button" @click="toggleCollapse" >|||</div>
                    <!-- 侧边栏菜单区 -->
                     <el-menu
                        background-color="#545c64"
                        text-color="#fff"
                        active-text-color="#409eff"
                        :unique-opened="true"
                        :collapse="isCollapse"
                        :collapse-transition='false'
                        :router="true"
                        :default-active="activeUrl">
                        <!-- 一级菜单 -->
                     <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">

                       <template slot="title">
                         <i :class="iconimg[item.id]"></i>
                         <span>{{item.authName}}</span>
                       </template>
                       
                       <!-- 二级菜单 -->
                              
                                <el-menu-item :index="'/'+son.path" v-for="son in item.children" :key="son.id" @click="savaUrl('/'+son.path)">
                                  <template slot="title">
                                      <i class="el-icon-menu"></i>
                                      <span>{{son.authName}}</span>
                                  </template>
                                </el-menu-item>
                              
                     </el-submenu>
                    
                   </el-menu>
                  </el-aside>
                  <!-- 主界面 -->
                  <el-main>  
                      <router-view/>
                  </el-main>
               </el-container>
        </el-container>
</template>

<script>
export default {
    data(){
      return{
          menulist:[],
          iconimg:{
              '125': 'el-icon-user',
              '103': 'el-icon-s-operation',
              '101': 'el-icon-goods',
              '102': 'el-icon-document',
              '145': 'el-icon-finished'
          },
        //   是否折叠
          isCollapse: false,
          sideWidth: '200px',
          activeUrl: ''
      }
    },
    created(){
      this.getMenuList();
      this.activeUrl = window.sessionStorage.getItem('activePath')
    },
    methods:{
        logout(){
            window.sessionStorage.clear();
            this.$router.push('/login');
        },
        // 获取所有菜单请求
        async getMenuList(){
          const{data:res} = await this.$http.get('menus');
          if(res.meta.status !== 200) return this.$message.error(res.meta.msg);
          this.menulist = res.data
        //   console.log(res);
        },
        // 左侧菜单伸缩
        toggleCollapse(){
            this.isCollapse = !this.isCollapse
            if(this.isCollapse){
                this.sideWidth = '64px'
            }else{
                this.sideWidth = '200px'
            }
        },
        savaUrl(str){
          window.sessionStorage.setItem('activePath',str)
          this.activeUrl = str
        }
    }
}
</script>

<style lang="less" scoped>
.home-container{
    height: 100%;
}
.el-header{
    background-color: #000000;
    display: flex;
    justify-content: space-between;
    padding-left: 0px;
    align-items: center;
    color: #fff;
    font-size: 20px;
    >div{
        display: flex;
        align-items: center;
        span{
            margin-left: 5px;
        }
        img{
            height: 100%;
        }
    }
}
.el-aside{
    transition: all 0.5s;
    background-color:#545c64;
    .el-menu{
        border-right: none;
    }
}
.el-main{
    background-color: #eaedf1;
}
.toggle-button{
    background-color: #4a5064;
    color: #fff;
    text-align: center;
    font-size: 10px;
    letter-spacing: 0.2em;
    line-height: 24px;
    cursor: pointer;
}
</style>